<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as AuthFactory;

class AuthenticateApi {

	/**
	 * The Guard implementation.
	 *
	 * @var Guard
	 */
	protected $auth;

	/**
	 * Create a new filter instance.
	 *
	 * @param  Guard  $auth
	 * @return void
	 */
	public function __construct(AuthFactory $auth){
		$this->auth = $auth;
	}

	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next){
		//验证
		if($this->auth->guard('api')->check()){
            return $next($request);
		}
        return $this->error();
		
	}
    
    private function error(){
        exit(json_encode([
            'status' => '-1',
            'msg' => '未登录'
        ], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
    }
}